import {
  PRODUCT_INFO,
  PRODUCT_MEDIA_LIST,
  PRODUCT_CLEAR,
  PRODUCT_LIKE_ADD,
  PRODUCT_LIKE_CANCEL,
  PRODUCT_LIKE_LIST,
  PRODUCT_CATE_LIST,
  PRODUCT_BRAND_LIST
}
  from '../constants/product'
import {createAction} from '../utils/redux'
import {
  API_GOODS_PRODUCT_INFO,
  API_GOODS_PRODUCT_MEDIAS,
  API_PRODUCT_LIKE_ADD,
  API_PRODUCT_LIKE_CANCEL,
  API_PRODUCT_LIKE_LIST,
  API_GOODS_CATE,
  API_GOODS_BRAND,
  API_GOODS_PRODUCT
} from '../constants/api'
import {CATE_SUB_LIST, CATE_SUB_LIST_CLEAR, CATE_VIDEO_LIST, CATE_HEIGHT_CLEAR} from '../constants/cate'
import fetch from '../utils/request'
import Taro from '@tarojs/taro'


export const dispatchProductInfo = (payload) => createAction({
  url: API_GOODS_PRODUCT_INFO,
  type: PRODUCT_INFO,
  payload,
  cb: (result) => {
    result.name = result.name.replace(/[?]/g, "");
    return result
  }
})

export const dispatchProductMediaList = (option) => {
  const {
    totalWidth, page, size
  } = option
  return createAction({
    url: API_GOODS_PRODUCT_MEDIAS,
    type: PRODUCT_MEDIA_LIST,
    payload: {
      page: page ? page : 1,
      size: size ? size : 50,
    },
    cb: (images) => {
      images = images.list.map(item => {
        let obj = {
          id: item.id,
          url: item.bannerImage.url,
          width: item.bannerImage.imageInfo.width,
          height: item.bannerImage.imageInfo.height
        }
        return obj
      })
      images = [...images, ...images, ...images, ...images]


      // for (let i = 0; i < col; i++) {
      //   list[i] = new Array(Math.floor(mediaList.length / 2))
      // }
      // for (let i in mediaList) {
      //   const {width,height,url} = mediaList[i]
      //   mediaList[i].height = totalWidth/col / width * height
      //   // let info = await this.fetchPicInfo(results[i].cover)
      //   // results[i].height = 165 / info.width * info.height
      //   // results[i].height = rects.width/col / info.width * info.height
      //   if (i < col) {
      //     list[i][0] = mediaList[i]
      //     heightArr.push(mediaList[i].height)
      //   } else {
      //     let minHeight = Math.min.apply(null, heightArr)
      //     let minHeightIndex = heightArr.indexOf(minHeight)
      //     let index = list[minHeightIndex].filter(Boolean).length
      //     list[minHeightIndex][index] = mediaList[i]
      //     heightArr[minHeightIndex] += mediaList[i].height
      //   }
      // }
      // for (let i = 0; i < col; i++) {
      //   list[i] = list[i].filter(Boolean)
      // }

      let heightArr = []
      let list = []
      let col = 3
      let scale = 2
      for (let i in images) {
        let height = totalWidth / col / images[i].width * images[i].height * scale
        images[i].showHeight = height
        // 第一行的两个盒子
        if (i < col) {
          list.push([images[i]])
          console.log('heightArr', heightArr)
          heightArr.push(height)
        } else {
          console.log('选出高度较矮的一列的索引')
          // 选出高度较矮的一列的索引
          let minHeight = Math.min.apply(null, heightArr)
          let minHeightIndex = heightArr.indexOf(minHeight)
          list[minHeightIndex].push(images[i])
          heightArr[minHeightIndex] += height
        }
      }
      console.log('瀑布流', list)
      return list
    }
  })

}


export const dispatchProductLikeAdd = (payload) => createAction({
  url: API_PRODUCT_LIKE_ADD,
  type: PRODUCT_LIKE_ADD,
  method: 'POST',
  payload,
  cb: (result) => {
    Taro.showToast({
      title: '收藏成功！',
      icon: 'success',
      duration: 500
    })
    return result
  }
})


export const dispatchProductLikeCancel = (payload) => createAction({
  url: API_PRODUCT_LIKE_CANCEL,
  type: PRODUCT_LIKE_CANCEL,
  method: 'POST',
  payload,
  cb: (result) => {
    Taro.showToast({
      title: '取消收藏！',
      icon: 'none',
      duration: 500
    })
    return result
  }
})

export const dispatchProductLikeList = (payload) => createAction({
  url: API_PRODUCT_LIKE_LIST,
  type: PRODUCT_LIKE_LIST,
  payload,
  cb: (result) => {
    return result
  }
})

export const dispatchProductClear = () => {
  return dispatch => {
    dispatch({type: PRODUCT_CLEAR})
  }
}

/**
 * @module product
 * @author: ymy
 * @description:分类列表
 * @since: 创建时间  2020-10-07 13:07:26
 */

export const dispatchCateList = (payload) => {
  return dispatch => {
    return fetch({
      url: API_GOODS_CATE,
      payload
    }).then(result => {
      return dispatch({
        type: PRODUCT_CATE_LIST,
        qianqian: {
          // menu:result.filter(item=>item.id != 1)
          cates: result
        }
      })
    })
  }
}

/**
 * @module product
 * @author: ymy
 * @description:品牌列表
 * @since: 创建时间  2020-10-07 13:07:26
 */

export const dispatchBrandList = () => {
  return dispatch => {
    return fetch({
      url: API_GOODS_BRAND,
      payload: {
        indexes: 1
      }
    }).then(result => {
      return dispatch({
        type: PRODUCT_BRAND_LIST,
        qianqian: {
          // menu:result.filter(item=>item.id != 1)
          brands: result
        }
      })
    })
  }
}


/**
 * @module cate
 * @author: ymy
 * @description:商品列表
 * @since: 创建时间  2020-09-18 17:53:36
 */
export const dispatchSubList = (payload) => {
  const option = {
    status: 1,
    page: payload.page ? payload.page : 1,
    size: payload.size ? payload.size : SIZE,
    banner: 0,
    order: payload.order ? payload.order : 'createTime',
    sort: payload.sort ? payload.sort : 'desc',
    cateIds: payload.cateIds ? payload.cateIds : '',
    brandIds: payload.brandIds ? payload.brandIds : '',
    name: payload.name ? payload.name : '',
    video: payload.video ? 1 : 0,
    keyword: payload.keyword || ''
  }
  const fetchOptions = option.page > 1 ? {delayTime: null} : {delayTime: null}
  return dispatch => {
    return fetch({
      url: API_GOODS_PRODUCT,
      payload: option,
      ...fetchOptions
    }).then(result => {
      return dispatch(action_product_list(result, payload))
    })
  }


  // return createAction({
  //   url: API_GOODS_PRODUCT,
  //   type: payload.video?CATE_VIDEO_LIST:CATE_SUB_LIST,
  //   payload:option,
  //   fetchOptions:option.page>1?{delayTime:300}:{delayTime:null},
  //   cb:(result)=>{
  //     // return result
  //   }
  // })
}

const action_product_list = (result, payload) => {
  let {list, pagination} = result
  let {subList, totalWidth, showMasonry, masonryList, heightArr, page, video} = payload
  if (showMasonry) {
    let images = list.map(item => {
      let obj = {
        id: item.id,
        width: item.bannerImage.imageInfo.width,
        height: item.bannerImage.imageInfo.height
      }
      return obj
    })
    let col = 2
    let scale = 1
    if (page == 1) {
      masonryList = []
      heightArr = []
    }
    for (let i in images) {
      let height = totalWidth / col / images[i].width * images[i].height * scale
      images[i].showHeight = height
      // 第一行的两个盒子
      if (i < col && page == 1) {
        masonryList.push([list[i]])
        heightArr.push(height)
      } else {
        // 选出高度较矮的一列的索引
        let minHeight = Math.min.apply(null, heightArr)
        let minHeightIndex = heightArr.indexOf(minHeight)
        masonryList[minHeightIndex].push(list[i])
        heightArr[minHeightIndex] += height
      }
    }
    if (page > 1) {
      list = [...subList, ...list]
    }
    return {
      type: video ? CATE_VIDEO_LIST : CATE_SUB_LIST,
      qianqian: {
        list, masonryList, pagination, heightArr
      }
    }
  } else {
    if (page > 1) {
      list = [...subList, ...list]
    }
    // console.log('list',list)
    return {
      type: video ? CATE_VIDEO_LIST : CATE_SUB_LIST,
      qianqian: {
        list, pagination, heightArr: [], masonryList: []
      }
    }
  }
}

export const dispatchSubListClear = () => {
  return dispatch => {
    dispatch({type: CATE_SUB_LIST_CLEAR})
  }
}

export const dispatchHeightArrClear = () => {
  return dispatch => {
    dispatch({type: CATE_HEIGHT_CLEAR})
  }
}
